﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenSSL.X509;
using System.Configuration;
using System.IO;
using System.Web;
using OpenSSL.Core;

namespace Sicurezza.CA.Manager
{
    public class FileManager
    {
        private static string certsPath = "";
        private static string reqsPath = "";
        private static string revsPath = "";
        private static string rensPath = "";
        private static string cacertPath = "";

        static FileManager()
        {
            certsPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,
                ConfigurationManager.AppSettings["certificates"]);
            reqsPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,
                ConfigurationManager.AppSettings["requests"]);
            revsPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,
                ConfigurationManager.AppSettings["revokes"]);
            rensPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,
                ConfigurationManager.AppSettings["renews"]);
            cacertPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath,
                ConfigurationManager.AppSettings["CaCertPath"]);
        }

        public static void SaveCertificate(X509Certificate cert)
        {

            string certPath = Path.Combine(certsPath, "" + cert.SerialNumber + ".pem");
            File.AppendAllText(certPath, cert.PEM);
        }

        public static void SaveRequest(int ReqId, X509Request r)
        {
            string reqPath = Path.Combine(reqsPath, "" + ReqId + ".pem");
            File.AppendAllText(reqPath, r.PEM);
        }

        public static string LoadRequest(int ReqId)
        {
            string reqPath = Path.Combine(reqsPath, "" + ReqId + ".pem");
            return File.ReadAllText(reqPath);
        }



        public static string LoadCertificate(int certId)
        {
            string certPath = Path.Combine(certsPath, "" + certId + ".pem");
            return File.ReadAllText(certPath);
        }

        public static void SaveRevoke(int RevId, X509Certificate cert)
        {
            string revPath = Path.Combine(revsPath, "" + RevId + ".pem");
            File.AppendAllText(revPath, cert.PEM);
        }

        public static void SaveRenew(int RenId, string newkey)
        {
            string renPath = Path.Combine(rensPath, "" + RenId + ".pem.key");
            File.AppendAllText(renPath, newkey);
        }

        public static string LoadRenew(int RenId)
        {
            string renPath = Path.Combine(rensPath, "" + RenId + ".pem.key");
            return File.ReadAllText(renPath);
        }


        public static string LoadCertificateFile(string path)
        {
            return File.ReadAllText(path);
        }
        public static string LoadCaCert()
        {
            return File.ReadAllText(cacertPath);
        }

    }
}
